﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WodLib.Controls
{
    public partial class InventoryEditorForm : Form
    {
        private WodLib.HumanInventory inventory;

        public InventoryEditorForm(WodLib.HumanInventory inventory)
        {
            InitializeComponent();
            itemBindingSource.CurrentItemChanged += new EventHandler(itemBindingSource_CurrentItemChanged);
            this.inventory = inventory;
        }

        void itemBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            propertyGridMain.SelectedObject = itemBindingSource.Current;
        }

        private void InventoryEditorForm_Load(object sender, EventArgs e)
        {
            //loads items from inventory to binding source
            foreach (var item in inventory.ItemsList)
            {
                itemBindingSource.Add(item);
            }
            WodLib.Tools.HierarchicalTypeFinder htf = new WodLib.Tools.HierarchicalTypeFinder();
            htf.FindTypes(typeof(WodLib.Item));
            foreach (var item in htf.TypesFound)
            {
                typeBindingSource.Add(item);
            }

        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            Item newItem = (Item)Activator.CreateInstance((Type)typeBindingSource.Current);
            newItem.Name = "--new--";
            itemBindingSource.Add(newItem);
        }

        private void InventoryEditorForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            inventory.ItemsList.Clear();
            foreach (var item in itemBindingSource)
            {
                inventory.ItemsList.Add((WodLib.Item)item);
            }
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            itemBindingSource.RemoveCurrent();
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WodLib.Tools.WodObjectClipboard.Copy((Item)itemBindingSource.Current);
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            itemBindingSource.Add(WodLib.Tools.WodObjectClipboard.Paste());
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            copyToolStripMenuItem_Click(sender, e);
            buttonRemove_Click(sender, e);
        }


    }
}
